package com.zwz.service.impl;
import com.zwz.entity.EnterpriseInfo;
import com.zwz.entity.InformationChange;
import com.zwz.mapper.InformationChangeMapper;
import com.zwz.service.InformationChangeService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;

@Service
@Transactional
public class InformationChangeServiceImpl implements InformationChangeService {

    @Autowired
    private InformationChangeMapper mapper;

    @Override
    public void create(InformationChange record) {

        // 创建主表记录
        EnterpriseInfo enterpriseInfo=new EnterpriseInfo();
        enterpriseInfo.setId(Long.valueOf(record.getEnterpriseId()));

        //最后关联上
        record.setEnterpriseId(Math.toIntExact(enterpriseInfo.getId()));
        validateChangeType(record.getChangeType());
        if(StringUtils.isEmpty(record.getChangeType())){
            record.setChangeType("地址变更");
        }
        mapper.insert(record);
    }

    @Override
    public InformationChange getById(Integer id) {
        return mapper.selectById(id);
    }

    @Override
    public void update(InformationChange record) {
        validateChangeType(record.getChangeType());
        mapper.update(record);
    }

    @Override
    public void deleteById(Integer id) {
        mapper.deleteById(id);
    }

    @Override
    public List<InformationChange> getAll() {
        return mapper.selectAll();
    }

    @Override
    public List<InformationChange> getByChangeType(String changeType) {
        validateChangeType(changeType);
        return mapper.selectByChangeType(changeType);
    }

    private void validateChangeType(String changeType) {
        //todo 以后加
//        if (!"地址变更".equals(changeType) &&
//                !"日期变更".equals(changeType) &&
//                !"其他".equals(changeType)) {
//            throw new IllegalArgumentException("非法的变更类型: " + changeType);
//        }
    }
}